import { Request, Response } from 'express';

// 房屋详情mock数据
const getHouseDetailData = (id: string) => ({
  id: id,
  // 房屋基础信息
  basicInfo: {
    cityName: '北京市',
    communityName: '华润城',
    address: '北京市朝阳区建国路88号华润城7栋3单元1502',
    houseInfo: '7栋3单元1502',
    area: 120,
    buildingType: '住宅',
    floor: 15,
    totalFloors: 24,
    orientation: '南北通透',
    decoration: '精装修',
    buildYear: 2020,
    propertyType: '商品房',
    propertyRight: '70年',
    createTime: '2024-01-15 10:30:00',
    updateTime: '2024-01-20 15:45:00',
  },
  // 出租信息
  rentalInfo: {
    rentalType: 'shared', // 'whole' | 'shared'
    status: '已出租',
    // 整租信息
    wholeRental: {
      monthlyRent: 8000,
      paymentCycle: '月付',
      rentalStatus: '已出租',
      viewingPeriod: '预约看房',
      moveInDate: '2024-02-01',
      tenantInfo: {
        name: '张三',
        phone: '138****8888',
        idCard: '110101199001011234',
        contractStartDate: '2024-02-01',
        contractEndDate: '2025-01-31',
      },
    },
    // 合租房间信息
    sharedRooms: [
      {
        id: 'room1',
        name: '房间A',
        roomType: '主卧',
        orientation: '南',
        area: 25,
        monthlyRent: 3000,
        status: '已出租',
        tenantInfo: {
          name: '李四',
          phone: '139****9999',
          contractStartDate: '2024-01-15',
          contractEndDate: '2024-12-14',
        },
      },
      {
        id: 'room2',
        name: '房间B',
        roomType: '次卧',
        orientation: '北',
        area: 18,
        monthlyRent: 2500,
        status: '未出租',
        tenantInfo: null,
      },
    ],
  },
  // 经营信息
  businessInfo: {
    propertyInfo: {
      hasPropertyInfo: true,
      propertyOwner: '王五',
      propertyCertificate: '京房权证字第123456号',
      propertyArea: 120,
      propertyType: '商品房',
      propertyRight: '70年',
      purchaseDate: '2020-05-15',
      purchasePrice: 1200000,
    },
    authInfo: {
      hasAuthInfo: true,
      authStatus: '已核验',
      authDate: '2024-01-10',
      authPerson: '赵六',
      authCertificate: '京房核验字第789012号',
      authPhotos: ['auth1.jpg', 'auth2.jpg'],
    },
    contractInfo: {
      contractAmount: 240000, // 季付
      contractStartDate: '2024-01-01',
      contractEndDate: '2024-12-31',
      serviceFee: 500,
      paymentCycle: '季付',
    },
    // 合同列表
    contracts: [
      {
        id: 'contract1',
        type: '租赁合同',
        tenantName: '张三',
        startDate: '2024-02-01',
        endDate: '2025-01-31',
        amount: 8000,
        status: '生效中',
        signDate: '2024-01-25',
      },
    ],
    // 账单列表
    bills: [
      {
        id: 'bill1',
        type: '租金',
        amount: 8000,
        dueDate: '2024-02-01',
        status: '已支付',
        payDate: '2024-01-30',
        description: '2024年2月租金',
      },
      {
        id: 'bill2',
        type: '水电费',
        amount: 350,
        dueDate: '2024-02-15',
        status: '待支付',
        payDate: null,
        description: '2024年1月水电费',
      },
    ],
    // 维修记录
    repairs: [
      {
        id: 'repair1',
        type: '水管维修',
        description: '厨房水管漏水',
        reportDate: '2024-01-20',
        status: '已完成',
        completeDate: '2024-01-22',
        cost: 200,
        repairman: '李师傅',
      },
    ],
    // 转租&退租申请
    transfers: [
      {
        id: 'transfer1',
        type: '退租申请',
        tenantName: '张三',
        applyDate: '2024-12-01',
        status: '待审核',
        reason: '工作调动',
        expectedDate: '2024-12-31',
      },
    ],
    // 操作日志
    logs: [
      {
        id: 'log1',
        action: '创建房屋信息',
        operator: '管理员',
        time: '2024-01-15 10:30:00',
        description: '新建房屋基础信息',
      },
      {
        id: 'log2',
        action: '更新出租信息',
        operator: '管理员',
        time: '2024-01-20 15:45:00',
        description: '修改租金为8000元/月',
      },
    ],
  },
});

export default {
  // 获取房屋详情
  'GET /api/house/detail/:id': (req: Request, res: Response) => {
    const { id } = req.params;

    // 模拟网络延迟
    setTimeout(() => {
      const data = getHouseDetailData(id);
      res.json({
        code: 200,
        message: '获取成功',
        data: data,
      });
    }, 300);
  },

  // 更新房屋基础信息
  'PUT /api/house/detail/:id/basic': (req: Request, res: Response) => {
    const { id } = req.params;

    setTimeout(() => {
      res.json({
        code: 200,
        message: '更新成功',
        data: { id },
      });
    }, 500);
  },

  // 更新出租信息
  'PUT /api/house/detail/:id/rental': (req: Request, res: Response) => {
    const { id } = req.params;

    setTimeout(() => {
      res.json({
        code: 200,
        message: '更新成功',
        data: { id },
      });
    }, 500);
  },
};
